package com.dawn.module.hospital.controller.admin.chs.config;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import com.dawn.framework.common.pojo.PageParam;
import com.dawn.framework.common.pojo.PageResult;
import com.dawn.framework.common.pojo.CommonResult;
import com.dawn.framework.common.util.object.BeanUtils;
import static com.dawn.framework.common.pojo.CommonResult.success;

import com.dawn.framework.excel.core.util.ExcelUtils;

import com.dawn.framework.apilog.core.annotation.ApiAccessLog;
import static com.dawn.framework.apilog.core.enums.OperateTypeEnum.*;

import com.dawn.module.hospital.controller.admin.chs.config.vo.*;
import com.dawn.module.hospital.dal.dataobject.chs.config.ChsConfigDO;
import com.dawn.module.hospital.service.chs.config.ChsConfigService;

@Tag(name = "管理后台 - 医保支付配置")
@RestController
@RequestMapping("/hospital/chs-config")
@Validated
public class ChsConfigController {

    @Resource
    private ChsConfigService chsConfigService;

    @PostMapping("/create")
    @Operation(summary = "创建医保支付配置")
    @PreAuthorize("@ss.hasPermission('hospital:chs-config:create')")
    public CommonResult<Long> createChsConfig(@Valid @RequestBody ChsConfigSaveReqVO createReqVO) {
        return success(chsConfigService.createChsConfig(createReqVO));
    }

    @PostMapping("/update")
    @Operation(summary = "更新医保支付配置")
    @PreAuthorize("@ss.hasPermission('hospital:chs-config:update')")
    public CommonResult<Boolean> updateChsConfig(@Valid @RequestBody ChsConfigSaveReqVO updateReqVO) {
        chsConfigService.updateChsConfig(updateReqVO);
        return success(true);
    }

    @GetMapping("/delete")
    @Operation(summary = "删除医保支付配置")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('hospital:chs-config:delete')")
    public CommonResult<Boolean> deleteChsConfig(@RequestParam("id") Long id) {
        chsConfigService.deleteChsConfig(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得医保支付配置")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('hospital:chs-config:query')")
    public CommonResult<ChsConfigRespVO> getChsConfig(@RequestParam("id") Long id) {
        ChsConfigDO chsConfig = chsConfigService.getChsConfig(id);
        return success(BeanUtils.toBean(chsConfig, ChsConfigRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得医保支付配置分页")
    @PreAuthorize("@ss.hasPermission('hospital:chs-config:query')")
    public CommonResult<PageResult<ChsConfigRespVO>> getChsConfigPage(@Valid ChsConfigPageReqVO pageReqVO) {
        PageResult<ChsConfigDO> pageResult = chsConfigService.getChsConfigPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, ChsConfigRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出医保支付配置 Excel")
    @PreAuthorize("@ss.hasPermission('hospital:chs-config:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportChsConfigExcel(@Valid ChsConfigPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<ChsConfigDO> list = chsConfigService.getChsConfigPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "医保支付配置.xls", "数据", ChsConfigRespVO.class,
                        BeanUtils.toBean(list, ChsConfigRespVO.class));
    }

}